package com.iot.ic.utils;

public class ToolUtils {

	/**
	 * 
	 * @param lat1
	 * @param lng1
	 * @param lat2
	 * @param lng2
	 * @return 两个经纬度之间的距离
	 */
	public static double getDistence(double lat1, double lng1, double lat2, double lng2) {
		double s = 0.0d;

		double EARTH_RADIUS = 6378137.0;
		double radLat1 = (lat1 * Math.PI / 180.0);
		double radLat2 = (lat2 * Math.PI / 180.0);
		double a = radLat1 - radLat2;
		double b = (lng1 - lng2) * Math.PI / 180.0;
		s = 2 * Math.asin(Math.sqrt(
				Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	// 经纬度转墨卡托
	// 经度(lon)，纬度(lat)
	public static double[] lonLat2Mercator(double lon, double lat) {
		double M_PI = Math.PI;

		double[] xy = new double[2];

		double x = lon * 20037508.342789 / 180;

		double y = Math.log(Math.tan((90 + lat) * M_PI / 360)) / (M_PI / 180);

		y = y * 20037508.34789 / 180;

		xy[0] = x;
		xy[1] = y;
		return xy;
	}

}
